{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Please install the itkwidgts before experiment\n",
    "details to install it can be found [here](https://github.com/InsightSoftwareConsortium/itkwidgets#installation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: scipy in /ssd2/tangshiyu/anaconda3/lib/python3.8/site-packages (1.6.2)\r\n",
      "Requirement already satisfied: numpy<1.23.0,>=1.16.5 in /ssd2/tangshiyu/anaconda3/lib/python3.8/site-packages (from scipy) (1.20.1)\r\n"
     ]
    }
   ],
   "source": [
    "# Install dependencies for this example\n",
    "# Note: This does not include itkwidgets, itself\n",
    "import sys\n",
    "!{sys.executable} -m pip install scipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from itkwidgets import view, compare\n",
    "\n",
    "# load the compressed data img=a, label=b, pred=c, infer=d\n",
    "data = np.load(\"figures/prediction.npz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(128, 128, 128)\n"
     ]
    }
   ],
   "source": [
    "# the infer output \n",
    "infer_result = data['infer'].squeeze()\n",
    "print(infer_result.shape)\n",
    "infer_result = infer_result*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "infer_result = infer_result.astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(128, 128, 128)\n"
     ]
    }
   ],
   "source": [
    "b = data['pred'].squeeze()\n",
    "print(b.shape)\n",
    "b = b*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "b = b.astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(128, 128, 128)\n"
     ]
    }
   ],
   "source": [
    "c = data['label'].squeeze()\n",
    "print(b.shape)\n",
    "c = c*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "c = c.astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(128, 128, 128)\n"
     ]
    }
   ],
   "source": [
    "d = data['img'].squeeze()\n",
    "print(d.shape)\n",
    "d = d*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "d = d.astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# b is pred, c is label, d is img\n",
    "predlabel = np.concatenate((b, c), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(128, 256, 128)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "762485dcc73340b0a2b23c6eee6eaf3f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(axes=True, geometries=[], gradient_opacity=1.0, point_sets=[], rendered_image=<itk.itkImagePython.itkIm…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(predlabel.shape)\n",
    "\n",
    "view(predlabel[:, 50:240, :],  axes=True, vmin=0, vmax=200, gradient_opacity=1.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2412dd34d0cf4e568b6ca1fe628485e6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(axes=True, cmap=['OrPu', 'BkGn'], geometries=[], gradient_opacity=1.0, interpolation=False, point_sets=…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(label_image=predlabel[:, 50:240, :], rotate=True, axes=True, vmin=0, vmax=200, gradient_opacity=10.0, cmap=[\"OrPu\", 'BkGn'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2168100f87274a93b1c1b2c331f06cb7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "AppLayout(children=(HBox(children=(Label(value='Link:'), Checkbox(value=True, description='cmap'), Checkbox(va…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "compare(b, c, link_cmap=True, rotate=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
